package com.zhe.zheblogmaster.config;

import com.zhe.zheblogmaster.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置视图控制器
 *
 * @author WHAOU
 * @date 2021/9/2
 * @email whaou@qq.com
 */
@Component
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login.html").setViewName("admin/login.html");
        registry.addViewController("/admin/index.html").setViewName("admin/index.html");
        registry.addViewController("/admin/category.html").setViewName("admin/category.html");
        registry.addViewController("/admin/editor.html").setViewName("admin/editor.html");
        registry.addViewController("/admin/blogs.html").setViewName("admin/blog.html");
        registry.addViewController("/admin/comment.html").setViewName("admin/comment.html");
        registry.addViewController("/admin/link.html").setViewName("admin/link.html");
        registry.addViewController("/admin/configuration.html").setViewName("admin/configuration.html");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/login").excludePathPatterns("/admin/dist/**").excludePathPatterns("/admin/plugins/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(BlogConstants.MD_IMAGE_URL + "**").addResourceLocations("file:" + BlogConstants.ROOT + BlogConstants.FILE_UPLOAD_DIC);
    }
}
